﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Board.Filters;
using Board.Models;

namespace Board.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class KardexController : Controller
    {
        //
        // GET: /Kardex/

        public WebUser currentUser = null;

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            currentUser = Tools.CurrentUser;
        }

        public ActionResult Index()
        {
            

            return View();
        }

        public ActionResult Kardex()
        {
            if (currentUser == null) return RedirectToAction("Login", "Account");
            string siteId = Request.Params["siteId"];
            string unitId = Request.Params["unitId"];
            bool showAll = "true".Equals(Request.Params["showAll"]);
            string refreshRateStr = Request.Params["refreshRate"];
            string refreshMethod = Request.Params["refreshMethod"];
            string pageStr = Request.Params["page"];
            string recordsPerPageStr = Request.Params["recordsPerPage"];
            bool isAjax = "true".Equals(Request.Params["isAjax"]);

            if (string.IsNullOrEmpty(siteId) && string.IsNullOrEmpty(unitId) && string.IsNullOrEmpty(pageStr) && string.IsNullOrEmpty(recordsPerPageStr)) showAll = true;

            ViewBag.SiteId = siteId;
            ViewBag.UnitId = unitId;
            ViewBag.ShowAll = showAll;

            
            

            
            if (!string.IsNullOrEmpty(refreshMethod) && Tools.RefreshMethodList.Contains(refreshMethod) && !string.IsNullOrEmpty(refreshRateStr))
            {
                try
                {
                    int refreshRate = int.Parse(refreshRateStr);
                    if (refreshRate > 20)
                    {
                        //Refresh set
                        ViewBag.Refresh = true;
                        ViewBag.RefreshRate = refreshRate;
                        ViewBag.RefreshMethod = refreshMethod;
                    }
                }
                catch { }
            }

            int page = 1;
            ViewBag.Page = 1;
            if (!string.IsNullOrEmpty(pageStr))
            {
                try
                {
                    page = int.Parse(pageStr);
                    if (page >= 1)
                    {
                        //page info
                        ViewBag.Page = page;
                    }
                }
                catch { }
            }

            int recordsPerPage = 20;
            ViewBag.RecordsPerPage = 20;
            if (!string.IsNullOrEmpty(recordsPerPageStr))
            {
                try
                {
                    recordsPerPage = int.Parse(recordsPerPageStr);
                    if (recordsPerPage < 1)
                    {
                        recordsPerPage = 20;
                    }
                }
                catch { }
            }

            ViewBag.IsBaby = false;
            using (DbEntities db = new DbEntities())
            {
                List<Site> KardexSites =
                    db.v_UserUnitPermission.Where(u => u.UserId == currentUser.UserId && u.BedType.Contains("Kardex"))
                      .Select(u=>u.SiteID).Distinct().ToList().Select(s=>new Site(s)).ToList();

                ViewBag.SiteDropdown = KardexSites.Select(u => new SelectListItem() { Text = u.SiteId, Value = u.SiteId, Selected = u.SiteId.Equals(siteId) });
                var showBeds = currentUser.GetShowBeds(showAll, siteId, unitId, refreshMethod, page, recordsPerPage).Select(s=>new{s.SiteID, s.UnitID, s.Bed}).ToList();
                //if (showBeds.Any() && !showBeds.Any(s => s.BedType.Equals("KardexMom")))
                //{
                //    ViewBag.IsBaby = true;
                //    ViewBag.Baby = (from b in db.v_KardexBaby
                //                    join s in showBeds on new { b.SiteID, b.UnitID, b.Bed } equals new { s.SiteID, s.UnitID, s.Bed }
                //                    orderby b.SiteID, b.UnitID, b.Bed
                //                    select b).ToList();
                //    return View();
                //}
                var mom = (from m in db.v_KardexMom

                           orderby m.SiteID, m.UnitID, m.Bed
                           select m).ToList();

                foreach (var vm in mom)
                {
                    if (!showBeds.Contains(new {vm.SiteID, vm.UnitID, vm.Bed})) mom.Remove(vm);
                }

                ViewBag.Mom = mom;
            }
            return View();
        }

        public ActionResult Modify()
        {
            string patientIdStr = Request.Params["patientId"];
            if (string.IsNullOrEmpty(patientIdStr))
            {
                return RedirectToAction("Kardex");
            }

            try
            {
                long patientId = long.Parse(patientIdStr);
                if (patientId <= 0) { return RedirectToAction("Kardex"); }
                using (DbEntities db = new DbEntities())
                {
                    var moms = db.v_KardexMom.Where(p => p.MomBoardID == patientId || p.BabyBoardID == patientId).ToList();
                    if (!moms.Any())
                    {
                        return RedirectToAction("Kardex");
                    }
                    ViewBag.Moms = moms;
                }
            }
            catch
            {
                return RedirectToAction("Kardex");
            }

            return View();
        }







    }
}
